﻿function checkAddressInfo(userName, address, phoneNumber) {
    var validInfo = false;
    if (userName.length == 0) {
        $("#userName").focus();
        $(".alert").attr("style", "display:block");
        $(".alert strong").html("请输入联系人姓名！");
    } else if (address.length == 0) {
        $("#address").focus();
        $(".alert").attr("style", "display:block");
        $(".alert strong").html("请输入详细收货地址！");
    } else if (!Val.isMobile(phoneNumber)) {
        $("#phoneNumber").focus();
        $(".alert").attr("style", "display:block");
        $(".alert strong").html("请输入正确的手机号！");
    } else {
        validInfo = true;
    }
    return validInfo;
}

function saveDeliveryAddrCallback(errorMsg) {
    $(".alert").attr("style", "display:block");
    $(".alert strong").html("保存收货地址失败！" + errorMsg);
}

function getTempSaveDeliveryAddress(deliveryAddr) {
    var deliveryAddrTemp = null;

    var userName = $("#userName").val();
    var address = $("#address").val();
    var phoneNumber = $("#phoneNumber").val();

    var validInfo = checkAddressInfo(userName, address, phoneNumber);
    if (true == validInfo) {
        var sex = true;
        if (document.getElementById("man").className.toString().indexOf("input_checked") > 0) {
            sex = true;
        } else {
            sex = false;
        }

        if (null != deliveryAddr) {
            deliveryAddrTemp = deliveryAddr;
            deliveryAddrTemp.username = userName;
            deliveryAddrTemp.deliveryaddr = address;
            deliveryAddrTemp.userphone = phoneNumber;
            deliveryAddrTemp.sex = sex;
        } else {
            var id = Math.floor(100000000000 + Math.random() * (9999999999999 - 1000000000000));
            deliveryAddrTemp = {
                'id': id,  // 收货地址信息id，8字节，客户端生成
                'username': userName,  // 用户称呼
                'sex': sex,  // 用户性别
                'userphone': phoneNumber,  // 用户手机号码
                'deliveryaddr': address  // 用户地址
            };
        }
    }
    return deliveryAddrTemp;
}